package com.hospital.repository;

import com.hospital.model.Doctor;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Repository
public class DoctorRepository {
    private final List<Doctor> doctors = new ArrayList<>();
    private Long nextId = 1L;

    @PostConstruct
    public void init() {
        doctors.add(new Doctor(1L, "Dr. Smith", "Cardiology"));
        doctors.add(new Doctor(2L, "Dr. Johnson", "Neurology"));
    }

    public Doctor save(Doctor doctor) {
        if (doctor.getId() == null) {
            doctor.setId(nextId++);
        }
        doctors.add(doctor);
        return doctor;
    }

    public Optional<Doctor> findById(Long id) {
        return doctors.stream().filter(d -> d.getId().equals(id)).findFirst();
    }

    public List<Doctor> findAll() {
        return new ArrayList<>(doctors);
    }
}